package indi.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import indi.woniu.entity.Pool;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.mapper.PoolMapper;
import indi.woniu.service.PoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wgt
 * Date: 2021/12/15:15:57
 * Description:
 */
@Service
public class PoolServiceImpl implements PoolService {
    @Autowired
    PoolMapper poolMapper;

    @Override
    //分页查询所有
    public ResponseEntity findAll(int page, int limit) {
        //page:第几页 limit:显示几行
        PageHelper.startPage(page, limit);
        List<Pool> all = poolMapper.findAll();
        PageInfo pageInfo = new PageInfo(all);
        ResponseEntity re = new ResponseEntity(200, "查询成功", pageInfo);
        return re;
    }

    @Override
    public ResponseEntity findByName(String name) {
        List<Pool> byName = poolMapper.findByName(name);
        ResponseEntity re;
        if (byName.isEmpty()) {
            re = new ResponseEntity(500, "查询失败", "查无此题库");
        } else {
            re = new ResponseEntity(200, "查询成功", byName);
        }


        return re;
    }

    //子题库查询
    @Override
    public ResponseEntity findByType(int page,int limit,Pool pool) {
        PageHelper.startPage(page,limit);

        List<Pool> sPool = poolMapper.findByType(pool);
        ResponseEntity re;
        if (sPool.isEmpty()) {
            re = new ResponseEntity(500, "查询失败", "数据为空");
        } else {


            PageInfo pageInfo=new PageInfo(sPool);
            re = new ResponseEntity(200, "查询成功", pageInfo);

        }
        return re;
    }


    //新增题库
    //判断是子库还是父库
    @Override
    public ResponseEntity fMovePool(Pool pool) {

        ResponseEntity re = null;



//        if (!allId.contains(pool.getFid())){
//        re = new ResponseEntity(500, "新增子题库失败", "查无此父题库");
        //如果fid不为空,则为子库
        if (pool.getFid() != null) {
            //查出所有子题库id
            List<Integer> allId = poolMapper.findAllId();
            //查出所有fid
            List<Integer> allFidPool = poolMapper.findAllFidPool();

//            如果此子题库中没有找到传过来的fid
            if (!allFidPool.contains(pool.getFid())){
         re = new ResponseEntity(500, "新增子题库失败", "查无此父题库");

            }else{

                //根据父id,创建父id下的子库
                int poolByFid = poolMapper.findPoolByFid(pool.getFid());
                //查询到所有父id
                List<Integer> allByFId = poolMapper.findAllByFId();
                //判断传进来的这个fid有没有父id
                if (allByFId.contains(pool.getFid())) {
                    // 把父id与子库的fid关联
                    pool.setId(poolByFid);
//            调用新增子库方法,创建子库并关联现有父库
                    int i = poolMapper.sMovePool(pool);
                    re = new ResponseEntity(200, "新增子库成功", i);
                } else {
                    //如果传进来的fid没有父id
                    re = new ResponseEntity(500, "新增子库失败", "子题库不能再添加子题库");

                }
            }



        } else {



            //fid为空,则为父库
            int i = poolMapper.fMovePool(pool);
            re = new ResponseEntity(200, "新增父库成功", i);
//
        }
//        }


        return re;


}
    //修改题库
    @Override
    public ResponseEntity  updatePool(Pool pool) {
        int i = poolMapper.updatePool(pool);
        ResponseEntity re=new ResponseEntity(200,"修改成功",i);
        return  re;


    }
    //删除题库
    @Override
    public ResponseEntity deletePoolById(int id) {
        ResponseEntity re;
        //查询父id
        Integer fid = poolMapper.findByFPool(id);
        System.out.println("fid="+fid);
//        如果父id为空,则为父id
        if (fid==null ||fid==0){
//            如果父库下没有子库,说明没有数据
            if (poolMapper.findBySPool(id).isEmpty()){
//                可以直接删除
                int i = poolMapper.deletePoolById(id);
                re=new ResponseEntity(200,"删除父题库成功",i);
//                return re;

//                如果有子库
            }else{
                // 根据父id 查到子id用list装起来
                List<Integer> sPoolId = poolMapper.findBySPool(id);
                System.out.println("子库id="+sPoolId);
//            根据子id,动态查询子库下有无题目
                int count = poolMapper.findQuestion(sPoolId);
                if(count!=0){
                    re=new ResponseEntity(500,"删除父题库和子题库失败","子题库下有题目"+count+"条");
                }else {
                    int dFPool = poolMapper.deletePoolById(id);
                    int counts=0;
                    int dSPool=0;
                    for (Integer zid : sPoolId) {
                         dSPool = poolMapper.deletePoolById(zid);
                         counts++;
                    }
                    re=new ResponseEntity(200,"删除父题库和子题库成功","成功删除"+dFPool+"条父题库和"+counts+"条子题库");

//                    return re;



                }
            }



        }else {
//            单独拿到子库id
//            int bySId = poolMapper.findBySId(id);
            //根据子库id查到子库下的题目总数


            int count = poolMapper.findBySPoolQuestion(id);
//                如果根据字库id查到的数据为0,则可以删除此字库
            if (count==0) {
                int i = poolMapper.deletePoolById(id);
                re=new ResponseEntity(200,"删除成功","成功删除"+i+"个子题库");
//                return re;
//                System.out.println("当然可以删除");
            }else{
//                System.out.println("当然不能删除");

                re=new ResponseEntity(520,"删除失败","子题库有"+count+"条题目");
//                return re;
            }
        }

        return re;
    }


//通过关键字查找所有父体库

    public ResponseEntity findFpool() {
        List<String> byNameFpool = poolMapper.findByNameFpool();
        Map map=new HashMap();

        for (String s : byNameFpool) {

                map.put(s,poolMapper.findSpoolByFName(s));
        }


        ResponseEntity re=null;
        if (byNameFpool==null){
             re=new ResponseEntity(500,"返回失败","父题库为空");
        }
        else{
           re=new ResponseEntity(200,"返回成功",map);

        }
        return re;
    }

    @Override
    public ResponseEntity findSpool(String poolName) {

        ResponseEntity re=null;
        List<String> spoolByFName = poolMapper.findSpoolByFName(poolName);

        if (spoolByFName.isEmpty()){
            re=new ResponseEntity(500,"返回失败","此父库下无子库");
        }else{
            re=new ResponseEntity(200,"返回成功",spoolByFName);

        }





        return re;
    }

    @Override
    public ResponseEntity findSpools() {
        ResponseEntity re=null;
        List<Integer> fpool = poolMapper.findFpool();
        List<Pool> spools = poolMapper.findSpools(fpool);
        if (spools.isEmpty()){
            re=new ResponseEntity(500,"返回失败","无数据");
        }else{
            re=new ResponseEntity(200,"返回成功",spools);

        }

        return re;
    }

    @Override
    public ResponseEntity findSFPool(String name) {

        ResponseEntity re;
        //查询所有父题库名字
        List<String> allFName = poolMapper.findAllFName();

        if (allFName.contains(name)){
            int sfPool = poolMapper.findSFPool(name);
            if (sfPool==0){
                re=new ResponseEntity(500,"返回失败","无此题库");
            }else{
                re=new ResponseEntity(200,"返回成功",sfPool);
            }
        }else{
            re=new ResponseEntity(500,"返回失败",name+"题库不存在");
        }

//        int sfPool = poolMapper.findSFPool(name);

        return re ;
    }

    @Override
    public ResponseEntity findFidPool(Pool pool) {
        List<Pool> fidPool = poolMapper.findFidPool(pool);
        ResponseEntity re;
        if (fidPool.isEmpty()){
            re=new ResponseEntity(500,"返回失败","无此题库");
        }else{
            re=new ResponseEntity(200,"返回成功",fidPool);
        }

        return re;
    }

    @Override
    public ResponseEntity insertFpool(Pool pool) {
        ResponseEntity re;
//        查到所有父体库
        List<String> allFName = poolMapper.findAllFName();
        //如果传过来的父体库名和数据库中父题库名一致,则题库重复,创建失败
        if (allFName.contains(pool.getPoolName())){
            re=new ResponseEntity(500,"创建失败","题库名重复");
        }else{
            int i = poolMapper.insertFpool(pool);
            re=new ResponseEntity(200,"返回成功","成功增加"+i+"个题库");

        }
        return re;
    }

    @Override
    public ResponseEntity insertSpool(Pool pool) {
        ResponseEntity re;
        //获取所有父题库id
        List<Integer> allFidPool = poolMapper.findAllFidPool();
//        如果父体库中没有pool写的fid,说明此父题库不存在
        if (!(allFidPool.contains(pool.getFid()))){
            re=new ResponseEntity(500,"创建失败","此父题库不存在");
        }else{
//            查找出pool的fid关联的父题库下的所有子题库的库名字
            List<String> fidSPoolName = poolMapper.findFidSPoolName(pool);
//            如果数据库中有这个名字,就重复了,创建失败
            if (fidSPoolName.contains(pool.getPoolName())){
                re=new ResponseEntity(500,"创建失败","子题库重名");
            }
            else{
                int i = poolMapper.insertSpool(pool);
                re=new ResponseEntity(200,"创建成功","成功增加"+i+"个题库");
            }
        }
        return re;
    }

    @Override
    public ResponseEntity findSonPoolName(Pool pool) {
        ResponseEntity re;
        List<Pool> sonPoolName = poolMapper.findSonPoolName(pool);
        if (sonPoolName.isEmpty()){
            re=new ResponseEntity(500,"查询失败","查无此题库");

        }else
        {
            re=new ResponseEntity(200,"查询成功",sonPoolName);

        }
        return re;
    }

    @Override
    public ResponseEntity findAllAll(int page,int limit) {
        PageHelper.startPage(page,limit);
        ResponseEntity re;
        List<Pool> allAll = poolMapper.findAllAll();
        if (allAll.isEmpty()){
            re=new ResponseEntity(500,"查询失败","无数据");

        }else{
            PageInfo pageInfo=new PageInfo(allAll);
            re=new ResponseEntity(200,"查询成功",pageInfo);

        }
        return re;
    }

    @Override
    public ResponseEntity findAllAllByName(Pool pool) {
        ResponseEntity re;
        List<Pool> allAllByName = poolMapper.findAllAllByName(pool);
        if (allAllByName.isEmpty()){
            re=new ResponseEntity(500,"查询失败","无数据");

        }else{
            re=new ResponseEntity(200,"查询成功",allAllByName);

        }
        return re;
    }

    @Override
    public ResponseEntity findByFidAndSunName(Pool pool) {
        ResponseEntity re;

        List<Pool> byFidAndSunName = poolMapper.findByFidAndSunName(pool);
        if (byFidAndSunName.isEmpty()){
            re=new ResponseEntity(500,"查询失败","无数据");

        }else{
            re=new ResponseEntity(200,"查询成功",byFidAndSunName);

        }

        return re;
    }


}
